using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Text;


namespace KaroGame.FrontEnd
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class DebugController : DrawableGameComponent
    {
        public static bool IsDebugMode { get; set; }

        private static StringBuilder debugViewInfo;
        private static StringBuilder debugGameInfo;
        private static StringBuilder debugAnimationAndErrorInfo;

        private KaroGame baseGame;
        private SpriteBatch spriteBatch;
        private SpriteFont fontText;
        private SpriteFont fontHeader;
        
        public DebugController(KaroGame game)
            : base(game)
        {
            baseGame = game;

            debugViewInfo       = new StringBuilder();
            debugGameInfo       = new StringBuilder();
            debugAnimationAndErrorInfo  = new StringBuilder();

            IsDebugMode = false;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        public new void LoadContent()
        {
            spriteBatch = baseGame.SpriteBatch;
            fontText    = baseGame.Content.Load<SpriteFont>("Font/DebugText");
            fontHeader  = baseGame.Content.Load<SpriteFont>("Font/DebugHeader");
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            debugViewInfo.Remove(0, debugViewInfo.Length);
            debugGameInfo.Remove(0, debugGameInfo.Length);
            debugAnimationAndErrorInfo.Remove(0, debugAnimationAndErrorInfo.Length);
        }

        /// <summary>
        /// Allows the game component to draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            if (IsDebugMode)
            {
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);

                int x = 20;
                spriteBatch.DrawString( fontHeader, "Game debug info:", new Vector2( x, 5 ), Color.White );
                spriteBatch.DrawString( fontText, debugGameInfo, new Vector2( x + 2, 30 ), Color.White );

                x = (baseGame.Window.ClientBounds.Width / 3 * 1) + 2;
                spriteBatch.DrawString( fontHeader, "Animation / Error debug info:", new Vector2( x, 5 ), Color.White );
                spriteBatch.DrawString( fontText, debugAnimationAndErrorInfo, new Vector2( x + 2, 30 ), Color.White );

                x = ( baseGame.Window.ClientBounds.Width / 3 * 2 ) + 2;
                spriteBatch.DrawString( fontHeader, "Viewing debug info:", new Vector2( x, 5 ), Color.White );
                spriteBatch.DrawString( fontText, debugViewInfo, new Vector2( x + 2, 30 ), Color.White );

                spriteBatch.End();
            }
        }

        public static void AddDebugViewInfo(string debugInfo)
        {
            debugViewInfo.AppendLine(debugInfo);
        }

        public static void AddDebugGameInfo(string debugInfo)
        {
            debugGameInfo.AppendLine(debugInfo);
        }

        public static void AddDebugAnimationOrErrorInfo(string debugInfo)
        {
            debugAnimationAndErrorInfo.AppendLine(debugInfo);
        }
    }
}